package api

import (
	"fiberdemo/app/model"
	"fiberdemo/app/service"
	"github.com/gofiber/fiber/v2"
	"net/http"
)

var User = newUserApi()

type userApi struct {
}

func newUserApi() *userApi {
	return &userApi{}
}

func (a *userApi) Login(ctx *fiber.Ctx) error {

	userLogin := new(model.UserLogin)

	//数据映射
	if err := ctx.BodyParser(userLogin); err != nil {
		//return ctx.JSON(fiber.Map{"code": http.StatusBadGateway, "msg": "参数错误"})
		return fiber.NewError(http.StatusInternalServerError, "参数错误")
	}

	//验证参数
	//if err := ValidateStruct(*userLogin); err != nil {
	//	return ctx.JSON(err)
	//}

	//调用服务
	flag := service.User.Login(ctx, userLogin.UserName, userLogin.Password)

	if flag {
		return ctx.JSON(fiber.Map{"code": http.StatusOK, "msg": "登录成功"})
	}

	return ctx.JSON(fiber.Map{"code": http.StatusNotFound, "msg": "登录失败"})
}
